package com.specialized.passenger.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.specialized.common.core.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.specialized.common.log.annotation.Log;
import com.specialized.common.log.enums.BusinessType;
import com.specialized.common.security.annotation.RequiresPermissions;
import com.specialized.passenger.domain.User;
import com.specialized.passenger.service.IUserService;
import com.specialized.common.core.web.controller.BaseController;
import com.specialized.common.core.utils.poi.ExcelUtil;

/**
 * 用户Controller
 *
 * @author djh
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/User")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

/**
 * 查询用户列表
 */
@RequiresPermissions("passenger:User:list")
@GetMapping("/list")
    public Result list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("passenger:User:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User. class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @RequiresPermissions("passenger:User:query")
    @GetMapping(value = "/{userId}")
    public Result getInfo(@PathVariable("userId") Long userId) {
        return success(userService.selectUserByUserId(userId));
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("passenger:User:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("passenger:User:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("passenger:User:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public Result remove(@PathVariable Long[] userIds) {
        return toAjax(userService.deleteUserByUserIds(userIds));
    }
}
